package com.umakr.ax.core.base;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;

import java.util.Map;

/**
 * @author gx
 */
public class ParameterParser {
    public enum QuerySplit{
        //field like "$value%"
         LK(".lk")
        //field like "value%"
        ,RLK(".rlk")
        //field like "%value"
        ,LLK(".llk")
        //  field = value
        ,EQ(".eq")
        //不等于 <>
        ,NE(".ne")
        //field>value
        ,GT(".gt")
        //field>=value
        ,GE(".ge")
        //field<value
        ,LT(".lt")
        //field<=value
        ,LE(".le")
        ;
        /**TODO : 部分还未实现*/
        private String sp;

        public String getSp() {
            return sp;
        }

        public void setSp(String sp) {
            this.sp = sp;
        }

        private QuerySplit(String sp){
            this.sp = sp;
        }
        public static QuerySplit fromName(String name){
            for(QuerySplit querySplit : QuerySplit.values()){
                if(querySplit.getSp().endsWith(name)){
                    return querySplit;
                }
            }
            return QuerySplit.EQ;
        }
    }

    public static  <T> Wrapper<T> parser(Map<String,Object> parameter){
        EntityWrapper<T> entityWrapper = new EntityWrapper<>();
        for(Map.Entry<String,Object> entry : parameter.entrySet()){
            String[] tls = entry.getKey().split("\\.");
            if(entry.getKey().startsWith("_q") && tls.length == 3){
                String key = tls[1];
                String qt = tls[2];
                switch (QuerySplit.fromName(qt)){
                    case LK :
                        entityWrapper.like(key,entry.getValue().toString());
                        break;
                    case RLK:
                        entityWrapper.like(key,entry.getValue().toString(), SqlLike.RIGHT);
                        break;
                    case LLK:
                        entityWrapper.like(key,entry.getValue().toString(), SqlLike.LEFT);
                        break;
                    case NE :
                        entityWrapper.ne(key,entry.getValue().toString());
                        break;
                    case GT :
                        entityWrapper.gt(key,entry.getValue().toString());
                        break;
                    case GE :
                        entityWrapper.ge(key,entry.getValue().toString());
                        break;
                    case LT :
                        entityWrapper.lt(key,entry.getValue().toString());
                        break;
                    case LE :
                        entityWrapper.le(key,entry.getValue().toString());
                        break;
                    default:
                        entityWrapper.eq(key,entry.getValue().toString());
                        break;
                }
            }


        }
        return entityWrapper;
    }
}
